require("../css/register.less");

const CaptchaMini= require("../lib/captcha-mini");

let count=""
document.ready(function(){
    let captcha1 = new CaptchaMini();
    captcha1.draw(document.querySelector('#captcha1'), r => {
        console.log(r, '验证码1');
        count=r
    });
    
 let phoneIpt=document.querySelector("#phone")
 let authCodeIpt=document.querySelector("#authCode")
 let psdIpt=document.querySelector("#psd")
 let psdAgainIpt=document.querySelector("#psdAgain")
 let submitBtn=document.querySelector("#submitBtn")
 let tips=document.querySelector(".tips")
 
 submitBtn.addEventListener("click",function(){
   let phone=phoneIpt.value
   let authCode=authCodeIpt.value
   let psdAgain=psdAgainIpt.value
   let psd=psdIpt.value

  if(!window.$utils.isphone(phone)){
  
    tips.textContent="手机号输入错误"
  }else if(authCode.toLowerCase() !=count.toLowerCase()){
      
      tips.textContent="验证码错误"
  }else if(!window.$utils.ispsd(psd)){
    tips.textContent="密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线"

  }else if(psdAgain!=psd){
      tips.textContent="两次输入密码不一致"


  }else{
      tips.textContent=""
      //   console.log("注册成功");
    
      axios.post("http://139.9.177.51:8099/users/add",
      {
          
            "account":phone,
            "password":psd,
     }
      )
      .then(function(res){
        let ret=res.data
        if(ret.status===0){
          
       
            
          window.$utils.toast("√","注册成功")
          
          window.setTimeout(function(){

            window.location.href="./login.html"
          },3200)
        }else if(ret.status===5001){
          
          window.$utils.toast("×","注册失败")
          
        }
      })
      .catch(function(err){
        console.log(err);
        
      })
    } 
  
 })
  
})
